package com.example.zq.player.linked_database_interface;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.zq.player.R;
import com.example.zq.player.linked_database_interface.base.RequestInterface;
import com.example.zq.player.linked_database_interface.base.ServerResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends Activity {
    String str_username;
    String str_passward;
    String str_email;
    String str_phone;
    String str_question;
    String str_answer;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.register);

        Button login_btn = findViewById(R.id.login_btn);
        Button registered_btn = findViewById(R.id.registered_btn);

        login_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str_username = ((EditText)findViewById(R.id.login_username)).getText().toString();
                String str_passward = ((EditText)findViewById(R.id.login_password)).getText().toString();
                String str_email = ((EditText)findViewById(R.id.login_email)).getText().toString();
                String str_phone = ((EditText)findViewById(R.id.login_phone)).getText().toString();
                String str_question = ((EditText)findViewById(R.id.login_question)).getText().toString();
                String str_answer = ((EditText)findViewById(R.id.login_answer)).getText().toString();

                RequestInterface.RegisterInterface register3 = GlobalInformation.getRetrofit().create(RequestInterface.RegisterInterface.class);
                Call<ServerResponse> call = register3.register(str_username, str_passward, str_email, str_phone, str_question, str_answer);
                call.enqueue(new Callback<ServerResponse>() {
                    @Override
                    public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
                        if (response.body() == null) {
                            Toast.makeText(RegisterActivity.this, "网络异常，请检查网络配置", Toast.LENGTH_SHORT).show();
                            return;
                        }
                        if (response.body().getStatus() == 0) {
                            //注册成功
                            Toast.makeText(RegisterActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();

                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            RegisterActivity.this.startActivity(intent);
                            RegisterActivity.this.finish();
                        }else{
                            //注册失败
                            Toast.makeText(RegisterActivity.this, response.body().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onFailure(Call<ServerResponse> call, Throwable t) {
                        Toast.makeText(RegisterActivity.this, "注册失败！", Toast.LENGTH_SHORT).show();
                        Log.d("mError", "RegisterFailure.");
                    }
                });
            }
        });
        registered_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                RegisterActivity.this.startActivity(intent);
                RegisterActivity.this.finish();
            }
        });
    }
}
